//
//  SLStatusCell.swift
//  langligelang
//
//  Created by 秦－政 on 2016/10/26.
//  Copyright © 2016年 pete. All rights reserved.
//

import UIKit

class SLStatusCell: UITableViewCell {
    // 记录底部视图的顶部添加约束
    var bottomViewTopCons :Constraint?
    // 定义一个SLStatusViewModel 属性
    var statusViewModel:SLStatusViewModel?{
        didSet{
            // 给原创微博赋值
            originolView.statusViewModel = statusViewModel
            // 卸载约束
            bottomViewTopCons?.uninstall()
            // 判断有没有转发微博
            if statusViewModel?.statusModel?.retweeted_status == nil {
                // 隐藏转发微博视图
                retweetView.isHidden = true
                // 没有转发微博 设置底部视图的约束
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomViewTopCons = make.top.equalTo(originolView.snp_bottom).constraint
                })
            }else{
                // 给转发微博赋值
                retweetView.statusViewModel = statusViewModel
                // 有转发微博 设置底部约束
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                // 显示转发微博视图
                retweetView.isHidden = false
            }
            // 给底部视图赋值
            bottomView.statusViewModel = statusViewModel
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        // 添加控件
        contentView.addSubview(originolView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        // 添加约束
        originolView.snp_makeConstraints { (make) in
            make.top.equalTo(contentView).offset(8)
            make.left.right.equalTo(contentView)
        }
        retweetView.snp_makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.top.equalTo(originolView.snp_bottom)
        }
        bottomView.snp_makeConstraints { (make) in
            // 记录底部视图的顶部约束
            self.bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(35)
            make.bottom.equalTo(contentView)
        }
    }
    // 懒加载控件
    // 原创微博
    private lazy var originolView: SLOriginalView = SLOriginalView()
    // 转发微博
    private lazy var retweetView:SLStatusRetweetView = SLStatusRetweetView()
    // 底部视图
    private lazy var bottomView: SLStatusBottomView = SLStatusBottomView()
    
}
